package com.group.mall.service.Impl;

import com.group.mall.domain.DTO.OrderDTO;
import com.group.mall.domain.POJO.Order;
import com.group.mall.domain.POJO.OrderDetail;
import com.group.mall.domain.POJO.User;
import com.group.mall.domain.VO.OrderShowVO;
import com.group.mall.mapper.OrdersMapper;
import com.group.mall.mapper.UserMapper;
import com.group.mall.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;
    //查询所有订单
    @Override
    public List<OrderDetail> getAllOrders(Long userId) {
        List<Order> orderList= ordersMapper.selectAllOrdersByUserId(userId);

        List<OrderDetail> OrderDetailList = new ArrayList<>();

        if(orderList!=null || orderList.size()!=0){
            OrderDetailList = listToOrderDetailList(orderList);
        }

        return OrderDetailList;
    }

    //根据订单状态查询订单
    @Override
    public List<OrderDetail> getOrderByStatus(Long userId, Integer status){
        //更具用户id和订单状态查询订单
        List<Order> orderList = ordersMapper.selectOrderByStatus(userId,status);

        List<OrderDetail> OrderDetailList = new ArrayList<>();

        if(orderList!=null || orderList.size()!=0){
            OrderDetailList = listToOrderDetailList(orderList);
        }
        return OrderDetailList;
    }


    //搜索订单
    @Override
    public List<OrderDetail> searchOrder(Long userId, String keyword){

        //查询所有订单
        List<Order> orderList= ordersMapper.selectAllOrdersByUserId(userId);

        List<OrderDetail> orderDetailList = new ArrayList<>();

        if(orderList!=null || orderList.size()!=0){
            //根据搜索信息查询订单详情
            orderDetailList = ordersMapper.selectOrderDetailByKeyword(keyword);
        }

        return orderDetailList;
    }

    //更新订单状态
    @Override
    @Transactional
    public Boolean updateOrderStatus(Long orderId, Integer status) {

        //根据订单id更新订单状态,状态，时间，更新时间，操作
        boolean result = updateOrder(status,orderId);

        return result;
    }

    //添加订单
    @Override
    @Transactional
    public OrderShowVO addOrder(OrderDTO orderDTO) {
        Order order = new Order();

        //新增订单表
        order.setCreateTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());
        order.setUserId(orderDTO.getUserId());
        order.setStatus(1);
        order.setTotalFee(orderDTO.getGoodsCount() * orderDTO.getGoodsPrice());
        ordersMapper.insertOrder(order);

        //新增订单详情表
        OrderDetail orderDetail = new OrderDetail();
        BeanUtils.copyProperties(orderDTO,orderDetail);
        orderDetail.setUserId(orderDTO.getUserId());
        orderDetail.setOrderId(order.getOrderId());
        orderDetail.setCreateTime(LocalDateTime.now());
        orderDetail.setUpdateTime(LocalDateTime.now());
        orderDetail.setOperation("去付款");
        ordersMapper.insertOrderDetail(orderDetail);


        OrderShowVO orderShowVO = new OrderShowVO();
        orderShowVO.setOrderTime(order.getCreateTime());
        orderShowVO.setOrderId(order.getOrderId());
        return orderShowVO;
    }

    @Override
    public Boolean cancelOrder(Long orderId) {
        Order order = ordersMapper.selectOrderByOrderId(orderId);

        boolean result = false;
        if(order!=null){
            //更新订单状态为取消
            result = updateOrder(5,orderId);
        }
        return result;
    }

    @Override
    public Boolean pay(Long orderId) {
        Order order = ordersMapper.selectOrderByOrderId(orderId);

        boolean result = false;
        if(order!=null){
            //更新订单状态为支付成功
            result = updateOrder(2,orderId);
            //更新用户余额
            User user = new User();
            user.setId(order.getUserId());
            user.setBalance(-(int)order.getTotalFee());
            userMapper.updateRecharge(user);
        }
        return result;
    }

    //退款
    @Override
    public Boolean refund(Long orderId) {
        Order order = ordersMapper.selectOrderByOrderId(orderId);

        boolean result = false;
        if(order!=null){
            //更新订单状态为支付成功
            result = updateOrder(5,orderId);
            //更新用户余额
            User user = new User();
            user.setId(order.getUserId());
            user.setBalance((int)order.getTotalFee());
            userMapper.updateRecharge(user);
        }
        return result;
    }

    //删除订单记录
    @Override
    public Boolean delate(Long orderId) {

        Boolean result = true;
        if(!ordersMapper.delateOrder(orderId)){
            result = false;
        }

        if(!ordersMapper.delateOrderDetail(orderId));

        return result;
    }

    //确认收货
    @Override
    public Boolean confirm(Long orderId) {
        Order order = ordersMapper.selectOrderByOrderId(orderId);

        boolean result = false;
        if(order!=null){
            //更新订单状态为已收货
            result = updateOrder(4,orderId);
        }
        return result;
    }

    //根据订单查询订单详情并封装
    private List<OrderDetail> listToOrderDetailList(List<Order> orderList){

        List<OrderDetail> orderDetailList = new ArrayList<>();

        for(Order order:orderList){
            log.info("order:{}",order);
            //查询订单细节
            OrderDetail orderDetail = ordersMapper.selectOrderDetailByOrderId(order.getOrderId());
            orderDetail.setStatus(order.getStatus());
            orderDetailList.add(orderDetail);
        }
        return orderDetailList;
    }

    private boolean updateOrder(Integer status,Long orderId){
        Order order = ordersMapper.selectOrderByOrderId(orderId);

        switch (status){
            case 2:
                //付款成功
                order.setStatus(2);
                order.setPayTime(LocalDateTime.now());
                order.setPaymentType(1);
                order.setUpdateTime(LocalDateTime.now());
                order.setOperation("退款");
                break;
            case 3:
                //发货成功
                order.setStatus(3);
                order.setConsignTime(LocalDateTime.now());
                order.setUpdateTime(LocalDateTime.now());
                order.setOperation("收货");
                break;
            case 4:
                //收货成功
                order.setStatus(4);
                order.setEndTime(LocalDateTime.now());
                order.setUpdateTime(LocalDateTime.now());
                order.setOperation("去评价");
                break;
            case 5:
                //取消，退款成功
                order.setStatus(5);
                order.setCloseTime(LocalDateTime.now());
                order.setUpdateTime(LocalDateTime.now());
                order.setOperation("删除记录");
                break;
            case 6:
                //评价成功,订单结束
                order.setStatus(6);
                order.setCommentTime(LocalDateTime.now());
                order.setUpdateTime(LocalDateTime.now());
                order.setOperation("删除记录");
                break;
        }

        if (!ordersMapper.updateOrderStatus(order)){
            return false;
        }
        //更新订单详情操作
        if(!ordersMapper.updateOrderDetailOperation(orderId,order.getOperation())){
            return false;
        }

        return true;
    }
}
